﻿using System;
using System.Linq;
using System.Reflection;

namespace Basmus.Values.Attributive
{
    public static class SchemeUtil
    {
        public static TAttribute GetKeyAttributeOrDefaultKeyAttribute<TMemberInfo, TAttribute>(this TMemberInfo memberInfo, SchemeKey key, bool inherit)
            where TMemberInfo : MemberInfo
            where TAttribute : Attribute, IScheme
        {
            var attrs = memberInfo
                .GetCustomAttributes(typeof (TAttribute), inherit)
                .Cast<TAttribute>();
            var result = attrs.FirstOrDefault(x => x.Key == key) ??
                         attrs.FirstOrDefault(x => x.Key == SchemeKey.Default);
            return result;
        }
    }
}
